﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Server
{
    class Program
    {
        private const int PORT = 31415;
        static void Main(string[] args)
        {
            try
            {
                ServerMonitor _serverMonitor = new ServerMonitor(); ;
                ServerThread _serverThread = new ServerThread(_serverMonitor);
                Thread clientThread;
                Thread hammerTimeThread;
                Socket clientSocket;
                IPEndPoint port = new IPEndPoint(IPAddress.Any, PORT);
                Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                int count = 0;
                serverSocket.Bind(port);
                serverSocket.Listen(10);

                hammerTimeThread = new Thread(_serverThread.HammerTimeHandle);

                while (true)
                {
                    clientSocket = serverSocket.Accept();
                    count++;
                    Console.WriteLine("Clients: {0}", count);
                    clientThread = new Thread(() => _serverThread.Handle(clientSocket));
                    clientThread.Start();

                    if (count == 1)
                    {
                        hammerTimeThread.Start();
                    }
                }
            }
            catch (IOException ioException) { }
        }
    }
}
